<template>
  <el-dialog
    :close-on-click-modal="false"
    :show-close="false"
    :title="isAdd?'新增分类':'修改分类'"
    :visible.sync="isEdit"
  >
    <el-form
      :rules="rules"
      ref="ruleForm"
      :model="form"
      label-width="120px"
      size="small"
      class="form-box"
    >
      <el-form-item label="分类名称" prop="name">
        <el-input type="text" v-model="form.name" placeholder="请输入分类名称"></el-input>
      </el-form-item>
      <el-form-item label="备注">
        <el-input type="textarea" v-model="form.desc" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="submitForClose('ruleForm')" size="small">取消</el-button>
      <el-button @click="submitFormAdd('ruleForm')" type="primary" size="small">确定</el-button>
    </span>
  </el-dialog>
</template>

<script lang="ts">
import { Component, Vue, Prop, Provide } from "vue-property-decorator";
import businessApi from "@/api/business";
@Component({
  components: {}
})
export default class typeEditDialog extends Vue {
  // 属性或参数中使用 ！：表示强制解析（告诉typescript编译器，这里一定有值），常用于vue-decorator中的@Prop
   check:boolean = true//防重复提交
  @Prop(Boolean) isEdit!: boolean;
  @Prop(Boolean) isAdd!: boolean;
  @Prop(Array) projectNames!: [];
  @Provide() dateTime: any = [];
  @Prop(Object) form!: {
    id:number,
    name:number,
    desc: string,
  };
  @Provide() rules: any = {
    name: [{ required: true, message: "请输入分类名称", trigger: "blur" }],
  };
  // 提交
  submitFormAdd(formName: any) {
    (this.$refs[formName] as any).validate((valid: boolean) => {
      if (valid) {
        if(this.check) {
          let _this = this
          _this.check = false
          businessApi.saveBusinessType(_this.form)
          .then((res: any) => {
            if(res.data.status){
              _this.$message({
                message: '操作成功',
                type: "success"
              });
              _this.$emit("closeDialog");
              _this.$emit("loadData");
              (_this.$refs[formName] as any).resetFields();
              setTimeout(function() {
                _this.check = true
              },1500)
            }else{
              _this.$message({
                message: res.data.msg,
                type: "error"
              });
              _this.check = true
            }
          });
        }
        
      }
    });
  };
  submitForClose(formName: any) {
    (this.$refs[formName] as any).resetFields();
    this.$emit("closeDialog");
  }
  created() {
  }
}
</script>

<style lang="scss"  >
.form-box {
  .el-input,
  .el-select {
    width: 200px !important;
  }
}
.el-textarea__inner{
  width: 200px;
}
</style>
